<template>
	<view class="pos-r">
		<view class="flex-r flex-start pos-r flex-fill" v-if="isbool(user)">
			<view class="">
				<image @tap="viewPic(pic(user.pic),[pic(user.pic)])" class="wh25-25" mode="aspectFill" :src="pic(user.pic)"></image>
			</view>
			<view class="flex1 padl3 fs8 lh2 c12">
				<view class="fwb fs12 c11 flrc">
					<view class="flex1">{{user.name}}</view>
					<view>#{{user.id}}</view>
				</view>
				<view><text>手机：</text>{{user.phone}}</view>
				<view><text>工号：</text>{{user.code}}</view>
				<view class="flrc">
					<view class="flex1"><text>部门：</text>{{isnull(user.department)?'未设置':user.department.name}}</view>
					<view>{{user.sex}} / {{user.age}} 岁 </view>
				</view>
			</view>
		</view>
		<view class="pad10" v-if="!isbool(user)">
			<view class="pad10 bdda tac c12">无效的数据</view>
		</view>
		<view class="pos-a l0 r0 t0 b0 flcc bcaf4 c12" v-show="loading && !isbool(user)">
			<text class="fa fa-spinner fa-spin fa-4x"></text>
		</view>
	</view>
</template>

<script>
	export default {
		props:{
			value:'',
			item:'',
			itemId:'',
			iri:'',
			code:''
		},
		data() {
			return {
				user:'',
				ready:false,
				loading:false,
			}
		},
		methods: {
			getById(val){
				this.loading = true
				this.$api.get("/api/users/" + val,{},res=>{
				this.loading = false
					this.log(res)
					this.user = res.d
				})
			},
			getByCode(code){
				this.loading = true
				this.$api.get("/api/users",{
					_where:{eq:{code}}
				},res=>{
				this.loading = false
					this.log(res)
					if(res.d.length == 1){
						this.user = res.d[0]
					}else{
						if(res.d.length == 0){
							this.$com.showToast('未找到设备')
						}else{
							this.$com.showToast('检测到重复的编号，请管理后台核对')
						}
					}
				})
			},
			update(){
				if(this.isbool(this.item)){
					this.user = this.item
					return
				}
				if(this.isbool(this.id)){
					this.getById(this.id)
					return
				}
				if(this.isbool(this.iri)){
					this.getById(this.iri.split("/").pop())
					return
				}
				if(this.isbool(this.code)){
					this.getByCode(this.code)
					return
				}
			}
		},
		mounted() {
			// #ifdef MP-WEIXIN || APP-PLUS
			if(this.isbool(this.value)) this.getById((this.value + '').split("/").pop())
			// #endif
			this.update()
		},
		watch:{
			id(val){
				if(!this.$com.isbool(val))return
				this.getById(val)
			},
			code(val){
				if(!this.$com.isbool(val))return
				this.getByCode(val)
			},
			value(val){
				if(!this.$com.isbool(val))return
				this.getById(val.split("/").pop())
			},
			user(user){
				if(!this.$com.isbool(user))return
				this.$emit('input','/api_/users/'+ user.id)
			}
		}
	}
</script>

<style>

</style>
